var ZYRequestClient = require("../../../utils/zy-request");
var requestClient;
var socketTask;
const app = getApp();
Page({
  data:{
    userInfo:{
      avatarUrl:'../../images/default-head.jpg',
      nickName:'授权登录',
      userAlias:'授权登录',
      companyName:''
    },
    navbarData:{
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  onShow(){
    var _this = this;
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo==''){
      wx.showModal({
        title:'授权登录',
        content:'请授权登录使用更多功能！',
        success(r){
          if(r.confirm){
            wx.navigateTo({
              url: '../../login/index',
            })
          }
        }
      })
      return;
    } else {
      // 获取默认公司
      requestClient.getUserDefaultCompany(userInfo.id,function(res){
        if(res.data.data == null){
            wx.showModal({
              title:'暂无企业',
              content:'请完善企业信息！',
              success(r){
                if(r.confirm){
                  wx.switchTab({
                    url: '../contacts/index',
                  })
                }
              }
            })
            return;
        } else {
          var defaultCompany = wx.getStorageSync('defaultCompany');
          // 刷新用户数据
          requestClient.getUserDetailInfoById(userInfo.id,function(res){
            _this.setData({
              'userInfo.avatarUrl':res.data.data.avatarUrl,
              'userInfo.nickName':res.data.data.nickName,
              'userInfo.userAlias':res.data.data.userAlias,
              'userInfo.companyName':defaultCompany.name
            });
            // 创建关闭websocket
            socketTask = requestClient.connectSocket();
            socketTask.onOpen(function(){
              // 连接成功获取审批状态
              var data = {
                'userId':userInfo.id,
                'operateType':'wait_approval'
              };
              socketTask.send({
                data:JSON.stringify(data)
              });
            });
            // 数据监听
            socketTask.onMessage(function(response){
              var messageData = JSON.parse(response.data);
              if(messageData.operateType == "wait_approval"){
                if(messageData.hasApproval){
                  wx.showTabBarRedDot({
                    index: 2,
                    text: ''
                  })
                } else {
                  wx.removeTabBarBadge({
                    index:2
                  });
                }
              }
            });
          });
        }
      });
    }
  },
  onHide(){
    // 关闭websocket
    if(socketTask){
      socketTask.close();
      socketTask = null;
    }
  },
  // 点击用户
  getUserInfo(e){
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo==''){
      wx.reLaunch({
        url: '../../login/index',
      })
    } else {
      wx.navigateTo({
        url: '../mine/account/index',
      })
    }
  },
  bindScanFile(e){
    wx.showLoading({
      title: '正在打开...',
    })
    var fileurl = e.currentTarget.dataset.fileurl;
    wx.downloadFile({
      url: fileurl,
      success: function (res) {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          fileType:'pdf',
          showMenu:true,
          success: function (res) {
            
          },
          fail:function(f){
            wx.showToast({
              title:'文档打开失败！',
              icon:'error'
            })
          },
          complete:function(c){
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      },
      fail:function(f){
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          title:'文档下载失败！',
          icon:'error'
        })
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh(e){
    this.onShow();
    wx.stopPullDownRefresh();
  }
})
